//
//  MySpaceViewController.m
//  HuateVideo
//
//  Created by  on 12-5-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MySpaceViewController.h"
#import "RecordViewController.h"
#import "LocationViewController.h"
#import "UserLoginViewController.h"
#import "UserRegiViewController.h"
#import "CustomTabBarController.h"
#define logined 0
@interface MySpaceViewController ()

@end

@implementation MySpaceViewController
static int SelectRow;
static MySpaceViewController* spaceViewController=nil;

+ (MySpaceViewController *) sharedInstance{
    @synchronized(self){
        if (spaceViewController == nil) {
            spaceViewController = [[self alloc] init];
        }
        return (spaceViewController);
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:USERLOGIN_ACTION object:nil];
    [tableArray release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshHeader:) name:USERLOGIN_ACTION object:nil];
        
      tableArray=[[NSMutableArray alloc] init];
        NSDictionary* d=[NSDictionary dictionaryWithObjectsAndKeys:@"lishi.png",@"img",LS(@"历史记录"),@"tit" ,nil];
        [tableArray addObject:d];
        d=[NSDictionary dictionaryWithObjectsAndKeys:@"myfile.png",@"img",LS(@"我的文件"),@"tit", nil];
      [tableArray addObject:d];
        d=[NSDictionary dictionaryWithObjectsAndKeys:@"shoucai.png",@"img",LS(@"我的收藏"),@"tit", nil];
        [tableArray addObject:d];
        d=[NSDictionary dictionaryWithObjectsAndKeys:@"xiazai.png",@"img",LS(@"我的下载"),@"tit", nil];
        [tableArray addObject:d];
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    UITableView* t=(UITableView*)[self.view viewWithTag:1111];
    [t deselectRowAtIndexPath:[NSIndexPath indexPathForRow:SelectRow inSection:0] animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    SelectRow=0;
	// Do any additional setup after loading the view.
    self.view.backgroundColor=TOOLBAR_TINTCOLOR;
    self.title=LS(@"我的空间");
    [self initHeader];
    [self initTable];
}
- (void)refreshHeader:(NSNotification*)notify{
    UIView* v=[self.view viewWithTag:10101];
    for (UIView* va in v.subviews) {
        [va removeFromSuperview];
    }
    UIBarButtonItem* rightB=[[UIBarButtonItem alloc] initWithTitle:LS(@"退出") style:UIBarButtonItemStylePlain target:self action:@selector(logout)];

    self.navigationItem.rightBarButtonItem=rightB;
    [rightB release];
    v.frame=CGRectMake(0, 0, 320, 80);
    table.frame=CGRectMake(0, 80, 320, 460-44-49);
    NSString* userLevel=LS(@"注册用户");
    NSString*userNametext=[@"  " stringByAppendingString:[notify object]];
    UILabel* userName=[[UILabel alloc] initWithFrame:CGRectMake(15, 20, 160, 20)];
    userName.textColor=LIGHT_COLOR;
    userName.text=userNametext;
    userName.font=[UIFont systemFontOfSize:15];
    userName.backgroundColor=[UIColor clearColor];
    [v addSubview:userName];
    [userName release];
    
    UIButton* bu=[UIButton buttonWithType:UIButtonTypeCustom];
    bu.frame=CGRectMake(220, 20, 65, 25);
    bu.titleLabel.font=[UIFont systemFontOfSize:15];
    [bu setTitleColor:LIGHT_COLOR forState:UIControlStateNormal];
    [bu setBackgroundImage:[UIImage imageNamed:@"sizianniu.png"] forState:UIControlStateNormal];
    [ bu  setTitle:LS(@"切换帐号") forState:UIControlStateNormal ];
    [v addSubview:bu];
    [bu addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel* la=[[UILabel alloc] initWithFrame:CGRectMake(15, 50, 300, 30)];
    la.text=[NSString stringWithFormat:@"%@:%@",LS(@"  登录方式"),userLevel];
    la.textColor=[UIColor lightGrayColor];
    la.backgroundColor=[UIColor clearColor];
    la.font=[UIFont systemFontOfSize:14];
    [v addSubview:la];
    [la release];
}
- (void)initHeader{
//    登陆了
    UIView*v=[self.view viewWithTag:10101];
    if (v==nil) {
        v=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
        //    登录信息  userLevel
        v.tag=10101;
        v.backgroundColor=[UIColor clearColor];
        [self.view addSubview:v];
        [v release];
    }
    
    for (UIView* va in v.subviews) {
        [va removeFromSuperview];
    }
    table.frame=CGRectMake(0, 120, 320, 460-44-49);

    UIBarButtonItem* rightB=[[UIBarButtonItem alloc] initWithTitle:LS(@"注册") style:UIBarButtonItemStylePlain target:self action:@selector(regist)];
    self.navigationItem.rightBarButtonItem=rightB;
    [rightB release];

    NSString* userLevel=LS(@"注册用户");
    NSString*userNametext=[NSString stringWithFormat:@"%@:%@",LS(@"  登录方式"),LS(@"  游客")];
    UILabel* userName=[[UILabel alloc] initWithFrame:CGRectMake(15, 20, 160, 20)];
    userName.textColor=LIGHT_COLOR;
    userName.text=userNametext;
    userName.font=[UIFont systemFontOfSize:15];
    userName.backgroundColor=[UIColor clearColor];
    [v addSubview:userName];
    [userName release];
    
    UIButton* bu=[UIButton buttonWithType:UIButtonTypeCustom];
    bu.frame=CGRectMake(220, 20, 65, 25);
    bu.titleLabel.font=[UIFont systemFontOfSize:15];
    [bu setTitleColor:LIGHT_COLOR forState:UIControlStateNormal];
    [bu setBackgroundImage:[UIImage imageNamed:@"sizianniu.png"] forState:UIControlStateNormal];
    [ bu  setTitle:LS(@"切换帐号") forState:UIControlStateNormal ];
    [v addSubview:bu];
    [bu addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];

    
    UILabel* la=[[UILabel alloc] initWithFrame:CGRectMake(15, 50, 300, 30)];
    la.text=[NSString stringWithFormat:@"%@%@",LS(@"  登录方式"),userLevel];
    la.textColor=[UIColor lightGrayColor];
    la.backgroundColor=[UIColor clearColor];
    la.font=[UIFont systemFontOfSize:14];
    [v addSubview:la];
    [la release];
//    未登陆
    //NSString *user = [UserDefaults objectForKey:USERNAME_STORE_KEY];
    if (!logined) {
        UILabel*reminder=[[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 20)];
        reminder.text=LS(@"  您为游客帐号，点击完善注册信息");
        reminder.font=[UIFont systemFontOfSize:15];
        reminder.textColor=[UIColor lightGrayColor];
        reminder.backgroundColor=LOW_COLOR;
        [v addSubview:reminder];
        [reminder release];
        
//        la.text=[NSString stringWithFormat:@"%@%@%@", LS(@"  登录方式:"),userLevel,LS(@"(登录后将永久保存个人信息)")];
        [ bu  setTitle:LS(@"登录") forState:UIControlStateNormal ];
        userName.frame=CGRectMake(15, 60, 160, 20);
        bu.frame=CGRectMake(200, 60, 75, 25);
//        la.frame= CGRectMake(15, 90, 320, 30);
        la.hidden=YES;
    }
}
-(void)logout{
    [UserDefaults setObject:@"" forKey:USERNAME_STORE_KEY];
    [self initHeader];
}
-(void)login{
    UserLoginViewController* login=[[UserLoginViewController alloc] init];
    UINavigationController* nav=[[UINavigationController alloc] initWithRootViewController:login];

    [self.navigationController presentModalViewController:nav animated:YES];
    [login release];
    [nav release];
}    
    
- (void)regist{
    UserRegiViewController* regist=[[UserRegiViewController alloc] init];
    UINavigationController* nav=[[UINavigationController alloc] initWithRootViewController:regist];
    [self.navigationController presentModalViewController:nav animated:YES];
//    [regist release];
    [nav release];
}

#pragma mark table
- (void)initTable{
    
    table=[[UITableView alloc] initWithFrame:CGRectMake(0, 120, 320, 460-44-49) style:UITableViewStylePlain];
    table.tag=20202;

    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 2)];
    line.image = [UIImage imageNamed:@"fengexian.png"];
    [table addSubview:line];
    [line release];

    table.delegate=self;
    table.dataSource=self;
    table.opaque=NO;
    table.tag=1111;
    table.scrollEnabled=NO;
    table.contentInset=UIEdgeInsetsMake(1, 0, 0, 0);
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    table.backgroundColor=[UIColor clearColor];
    [self.view addSubview:table];
    [table release];
    UIView* v=[[UIView alloc] init];
    table.tableFooterView=v;
    [v release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [tableArray count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString* ListIdentifier = @"ListIdentifier";
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ListIdentifier];
	
	if (cell == nil)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ListIdentifier] autorelease];
		//cell.selectionStyle = UITableViewCellSelectionStyleNone;  //去掉选中时的颜色
        UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 49, 320, 2)];
        line.image = [UIImage imageNamed:@"fengexian.png"];
        [cell.contentView addSubview:line];
        [line release];
	}
    NSDictionary*d=[tableArray objectAtIndex:indexPath.row];
    cell.textLabel.text=[d valueForKey:@"tit"];
    cell.imageView.image=[UIImage imageNamed:[d valueForKey:@"img"]];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:16.0];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectRow=indexPath.row;
    switch (indexPath.row) {
        case 0:
        {
            RecordViewController* rec=[[RecordViewController alloc] init];
            rec.title=LS(@"历史记录");
			rec.isPlayRecord=YES;
            [self.navigationController pushViewController:rec animated:YES];
            [rec release];
        }
            break;
        case 1:
        {
            [(CustomTabBarController *)self.tabBarController setSelectedItemIndex:2];
        }
            break;
        case 2:
        {
            RecordViewController* rec=[[RecordViewController alloc] init];
            rec.title=LS(@"我的收藏");
			rec.isPlayRecord=NO;
            [self.navigationController pushViewController:rec animated:YES];
            [rec release];
        }
            break;
        case 3:
        {
			AppDelegate* appdele=(AppDelegate*)[[UIApplication sharedApplication] delegate];
            appdele.downLoadedViewController.title = LS(@"我的下载");
            [self.navigationController pushViewController:appdele.downLoadedViewController animated:YES];
        }
            break;
            
        default:
            break;
    }

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
